
package com.epam.android.training.day10;

import com.epam.android.training.day10.dao.SecretContactsDAO;
import com.epam.android.training.day10.dao.SecretContactsDAO.SecretContacts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author pavel_busko
 * 
 * Activity to test shared prefs and SQL lite.
 */
public class Day10Activity extends Activity {

    public static final int RESULT_DELETE = 123;

    public static final int RESULT_UPDATE = 124;

    public static final String NAME_KEY = "name";

    public static final String MAIL_KEY = "mail";

    private static final String ID = "ID";

    private static final int EDIT_ACTIVITY_REQUEST_CODE = 0;

    private static final String PREFS_NAME = "ContactsPrefs";

    SecretContactsDAO secretContactsDAO;

    SimpleCursorAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        sharedPreferences.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.new_shared_pref, key, sharedPreferences.getString(key, "")),
                        Toast.LENGTH_SHORT).show();
            }
        });
        setContentView(R.layout.main);
        final EditText editName = (EditText) findViewById(R.id.edit_name);
        final EditText editMail = (EditText) findViewById(R.id.edit_email);

        Button buttonToSharedPrefs = (Button) findViewById(R.id.button_to_shared_prefs);
        buttonToSharedPrefs.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Editor edit = sharedPreferences.edit();
                edit.putString(NAME_KEY, editName.getText().toString());
                edit.putString(MAIL_KEY, editMail.getText().toString());
                edit.commit();
            }
        });

        secretContactsDAO = new SecretContactsDAO(getApplicationContext());
        adapter = new SimpleCursorAdapter(this, R.layout.list_item, secretContactsDAO.readAll(), new String[] {
                SecretContacts.NAME, SecretContacts.MAIL
        }, new int[] {
                R.id.name_text, R.id.mail_text
        });

        Button buttonSaveToDatabase = (Button) findViewById(R.id.button_save_to_database);
        buttonSaveToDatabase.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                secretContactsDAO.save(editName.getText().toString(), editMail.getText().toString());
                adapter.changeCursor(secretContactsDAO.readAll());
            }
        });

        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
        // listView.setItemsCanFocus(false);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), EditItemActivity.class);
                intent.putExtra(ID, id);
                intent.putExtra(NAME_KEY, ((TextView) view.findViewById(R.id.name_text)).getText());
                intent.putExtra(MAIL_KEY, ((TextView) view.findViewById(R.id.mail_text)).getText());
                startActivityForResult(intent, EDIT_ACTIVITY_REQUEST_CODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        // check if back button wasn't pressed
        if (intent != null) {
            long id = intent.getLongExtra(ID, -1);
            if (id != -1) {
                if (resultCode == RESULT_DELETE) {
                    secretContactsDAO.delete(id);
                } else if (resultCode == RESULT_UPDATE) {
                    secretContactsDAO.update(id, intent.getCharSequenceExtra(NAME_KEY).toString(), intent
                            .getCharSequenceExtra(MAIL_KEY).toString());
                }
                adapter.changeCursor(secretContactsDAO.readAll());
            } else {
                throw new IllegalStateException("Id of item for update is not correct!");
            }
        }
    }
}
